package net.chat;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Class representing a forum.
 * @author Prabhat
 */
public class Forum {

    // attributes
    private static int lastFid = 0;
    private int fid;
    private String name;
    private String category;
    private Date created;
    private List<User> users; // users in the forum
    private List<Message> oldMessages; // messages delivered to all users
    private List<Message> newMessages; // new messages, still not delivered to all
    // or some users

    // constructor
    public Forum(String name, String category) {
        this.fid = lastFid + 1;
        lastFid = this.fid;
        this.name = name;
        this.category = category;
        this.created = new Date();
        this.users = new ArrayList<User>();
        this.oldMessages = new ArrayList<Message>();
        this.newMessages = new ArrayList<Message>();
    }

    // getters
    public int getFid() {
        return fid;
    }

    public String getName() {
        return name;
    }

    public String getCategory() {
        return category;
    }

    public Date getCreated() {
        return created;
    }

    public List<User> getUsers() {
        return users;
    }

    public List<Message> getOldMessages() {
        return oldMessages;
    }

    public List<Message> getNewMessages() {
        synchronized (newMessages) {
            // if new messages delivered to all users, move it to old msg list
            for (Iterator<Message> itr = newMessages.iterator(); itr.hasNext();) {
                Message m = itr.next();
                List<User> deliveredUsers = m.getDeliveredUsers();

                if (deliveredUsers.containsAll(users)) {
                    oldMessages.add(m);
                    itr.remove();
                }
            }
        }
        return newMessages;
    }

    // setters
    public void setFid(int fid) {
        this.fid = fid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void setOldMessages(List<Message> oldMessages) {
        this.oldMessages = oldMessages;
    }

    public void setNewMessages(List<Message> newMessages) {
        this.newMessages = newMessages;
    }

    // methods
    // add new message to the list
    public void newMessage(Message message) {
        this.newMessages.add(message);
    }

    // add new user to the list of users in the forum. it first checks if user
    // is not already in list to avoid duplicate.
    public boolean join(User user) {
        synchronized (users) {
            user.setLastChat(new Date());
            if (isUserInForum(user.getUid())) {
                return false;
            } else {
                users.add(user);
            }
            return true;
        }
    }

    // remove user from the forum
    public void leave(User user) {
        synchronized (users) {
            if (users.contains(user)) {
                user.setLastChat(null);
            }
            users.remove(user);
        }
    }

    // check if the user is in the forum
    private boolean isUserInForum(int uid) {
        boolean result = false;
        for (User user : users) {
            if (user.getUid() == uid) {
                result = true;
                break;
            }
        }
        return result;
    }
}
